package cn.elwy.rcp.ui.config;

import java.util.Map;

import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.configuration.tree.ConfigurationNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.elwy.common.config.XmlConfig;
import cn.elwy.osgi.config.Constant;
import cn.elwy.rcp.core.model.ui.DialogInfo;
import cn.elwy.rcp.core.model.ui.DialogType;
import cn.elwy.rcp.ui.util.SWTUtil;

public class DialogConfigParser {

	protected Logger logger = LoggerFactory.getLogger(DialogConfigParser.class);

	private XmlConfig xmlConfig = new XmlConfig(getClass());
	private XMLConfiguration config;

	public void load() {
		xmlConfig.load(Constant.DIALOG_CONFIG_FILE);
		config = xmlConfig.getConfig();
		initDialogInfo();
	}

	protected void initDialogInfo() {
		ConfigurationNode rootNode = config.getRootNode();
		int childrenList = rootNode.getChildrenCount();
		Map<DialogType, DialogInfo> dialogInfoMap = DialogConfigFactory.getDialogInfoMap();
		for (int i = 0; i < childrenList; i++) {
			DialogInfo dialogInfo = new DialogInfo();
			String nodeName = "view(" + i + ")";

			String id = config.getString(nodeName + "[@id]");
			dialogInfo.setId(id);
			String type = config.getString(nodeName + "[@type]");
			dialogInfo.setType(type);

			boolean canCustomConfig = config.getBoolean(nodeName + "[@canCustomConfig]", false);
			dialogInfo.setCanCustomConfig(canCustomConfig);

			String style1 = config.getString(nodeName + ".style");
			int style = SWTUtil.toSWTConstant(style1, "NONE");
			dialogInfo.setStyle(style);

			boolean center = config.getBoolean(nodeName + ".location[@center]", true);
			dialogInfo.setCenter(center);
			int x = config.getInt(nodeName + ".location[@x]", 0);
			dialogInfo.setX(x);
			int y = config.getInt(nodeName + ".location[@y]", 0);
			dialogInfo.setY(y);

			boolean maximized = config.getBoolean(nodeName + ".maximized", true);
			dialogInfo.setMaximized(maximized);

			int width = config.getInt(nodeName + ".width", 600);
			dialogInfo.setWidth(width);
			int height = config.getInt(nodeName + ".height", 500);
			dialogInfo.setHeight(height);
			int minimumWidth = config.getInt(nodeName + ".minimumWidth", 500);
			dialogInfo.setMinimumWidth(minimumWidth);
			int minimumHeight = config.getInt(nodeName + ".minimumHeight", 500);
			dialogInfo.setMinimumHeight(minimumHeight);

			boolean canSetting = config.getBoolean(nodeName + ".canSetting", true);
			dialogInfo.setCanSetting(canSetting);

			boolean canEditor = config.getBoolean(nodeName + ".canEditor", true);
			dialogInfo.setCanEditor(canEditor);

			boolean canSave = config.getBoolean(nodeName + ".canSave", true);
			dialogInfo.setCanSave(canSave);
			DialogType dialogType = null;
			try {
				dialogType = DialogType.valueOf(type.toUpperCase());
			} catch (Exception e) {
				dialogType = DialogType.SWT;
			}
			dialogInfoMap.put(dialogType, dialogInfo);
		}
	}

}